package com.zw.pdm.module.craft.service;

import com.zw.pdm.entity.craft.CraMakeDetail;
import com.zw.pdm.entity.craft.CraMakeSheet;
import com.zw.pdm.entity.production.*;
import com.zw.pdm.entity.sys.User;
import com.zw.pdm.module.craft.mapper.CraftReceiveMapper;
import com.zw.pdm.module.craft.model.CarMakeResultModel;
import com.zw.pdm.module.craft.model.ServiceTaskModel;
import com.zw.pdm.module.report.mapper.ChangeMapper;
import com.zw.pdm.module.report.mapper.PlanMapper;
import com.zw.pdm.module.system.service.LogService;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * @Description:
 * @Author: China.sgl
 * @Date: 2019/12/6 08:42
 */
@Service
@SuppressWarnings("all")
public class CraftReceiveService {

    @Autowired
    private PlanMapper planMapper;
    @Autowired
    private CraftReceiveMapper craftReceiveMapper;
    @Autowired
    private ChangeMapper changeMapper;
    @Autowired
    private LogService logService;
    /**
     * 接收列表查询
     */
    public List<ServiceTaskModel> findWithCraftQuery(ServiceTaskModel serviceTaskModel) {
//        List<ServiceTaskModel> aa = craftReceiveMapper.findWithCraftQuery(serviceTaskModel);
        return craftReceiveMapper.findWithCraftQuery(serviceTaskModel);
    }

    /**
     * 查询选中部分明细(制作件)
     *
     * @param serviceTaskModel
     * @return
     */
    public List<ServiceTaskModel> findMakeSheetCraft(ServiceTaskModel serviceTaskModel) {
        return craftReceiveMapper.findMakeSheetCraft(serviceTaskModel);
    }

    /**
     * 查询选中部分明细（图纸）
     *
     * @param serviceTaskModel
     * @return
     */
    public List<ServiceTaskModel> findDrawSheetCraft(ServiceTaskModel serviceTaskModel) {
        return craftReceiveMapper.findDrawSheetCraft(serviceTaskModel);
    }

    /**
     * 接收数据到工艺表
     *
     * @param arrId
     * @param arrSheetType
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int receiveDesign(String[] arrId, String[] arrSheetType) {
        int result = 1;
        for (int i = 0; i < arrSheetType.length; i++) {

            switch (arrSheetType[i]) {
                case "0":
                    //图纸变更接收
                    List<DrawChange> listDrawChange = craftReceiveMapper.selectDrawChangByID(arrId[i]);
                    //判断工艺明细表是否有变更的数据，如果有可以接收，如果没有，提示先接上一单（针对变更新增清单后改图纸）
                    List<MakeResult> makeResultList = craftReceiveMapper.selectMakeResultByDrawSheetId(Integer.valueOf(arrId[i]));
                   if (makeResultList.size()>0){
                       return  -1;
                   }
                   //判断  工艺明细表中是否有 此数据
                   if (craftReceiveMapper.selectCraMakeDetail(Integer.valueOf(arrId[i]))>0){
                       return  -1;
                   }
                   //修改图纸变更主表状态，未接收变已接收，接收人为登录人，接收时间为当前时间
                    updateDrawSheet(arrId[i]);
                   //接收图纸变更，更新工艺制作件清单明细表 变更状态
                    for (int j = 0; j < listDrawChange.size(); j++) {
                        updateDrawChange(listDrawChange.get(j).getMakeResultId());
                    }
                    logService.saveLog("接收图纸变更", "设计图纸变更主表ID：" +arrId[i]);
                    break;
                case "1":
                    //原始单接收
                    List<CraMakeSheet> listCrasheet = craftReceiveMapper.selectCrasheetById(arrId[i]);
                    if (listCrasheet.size() > 0) {
                        //接收原始单，创建工艺制作件清单主表
                        insertCrasheet(listCrasheet.get(0));
                        //原始单接收置位，未接收变已接收，接收人为登录人，接收时间为当前时间
                        updateMakeSheet(arrId[i]);
                        List<CraMakeDetail> listCradetail = craftReceiveMapper.selectMakePlanByID(arrId[i]);
                        if (listCradetail.size() > 0) {
                            insertCraDetail(listCradetail);
                        }
                    }
                    logService.saveLog("接收制作件清单原始单", "设计制作件清单主表ID：" +arrId[i]);
                    break;
                case "2":
                    //制作件变更接收
                    List<CraMakeSheet> listCrasheet1 = craftReceiveMapper.selectCrasheetById(arrId[i]);
                    if (listCrasheet1.size() > 0) {
                        //更新接收状态
                        updateMakeSheet(arrId[i]);
                        //更新工艺项目数量
                        UpadteCraSheetProNum(listCrasheet1.get(0));
                        List<CarMakeResultModel> carMakeResultModel = craftReceiveMapper.selectCraftReceive(arrId[i]);
                        if (carMakeResultModel.size() > 0) {
                            updateCraDetail(carMakeResultModel, carMakeResultModel.get(0).getId());
                        }
                    }
                    logService.saveLog("接收制作件清单变更单", "设计制作件清单主表ID：" +arrId[i]);
                    break;
                default:
                    break;
            }
        }
        return result;
    }

    /**
     * 更改主表和附表方法
     *
     * @param id        主键ID
     * @param sheetType 修改类型（图纸变更、图纸目录变更）
     */
    public Integer updateDrawChange(Integer resultId) {
        return craftReceiveMapper.updatCraDetail(resultId);
    }

    /**
     * 接收数据插入到工艺主表
     *
     * @param craMakeSheet
     */
    public Integer insertCrasheet(CraMakeSheet craMakeSheet) {
        return craftReceiveMapper.insertCraSheet(craMakeSheet);
    }

    /**
     * 接收后反写make_sheet表状态
     */
    public Integer updateMakeSheet(String sheetId) {
        User u = ShiroUtils.getUser();
        MakeSheet makeSheet = new MakeSheet();
        makeSheet.setReceiveName(u.getName());
        makeSheet.setId(Integer.parseInt(sheetId));
        return craftReceiveMapper.updateMakeSheet(makeSheet);
    }

    /**
     * 接收后反写draw_sheet表状态
     */
    public Integer updateDrawSheet(String sheetId) {
        User u = ShiroUtils.getUser();
        DrawSheet drawSheet = new DrawSheet();
        drawSheet.setReceiveName(u.getName());
        drawSheet.setId(Integer.parseInt(sheetId));
        return craftReceiveMapper.updateDrawSheet(drawSheet);
    }

    /**
     * 接收数据到工艺明细表
     *
     * @param listCradetail
     */
    public void insertCraDetail(List<CraMakeDetail> listCradetail) {
        for (int i = 0; i < listCradetail.size(); i++) {
            if (listCradetail.get(i).getMakeType().trim().equals("装配件")) {
                listCradetail.get(i).setIsNeedCard(0);
            } else {
                listCradetail.get(i).setIsNeedCard(1);
            }
            listCradetail.get(i).setChangeType(0);
            listCradetail.get(i).setTechnologyChange(0);
            listCradetail.get(i).setLastTime(new Date());
            listCradetail.get(i).setCreatName(ShiroUtils.getUser().getName());
            craftReceiveMapper.insertCraDetail(listCradetail.get(i));
        }

    }

    /**
     * 修改接收的部分对应项目数量
     *
     * @param craMakeSheet
     * @return
     */
    public Integer UpadteCraSheetProNum(CraMakeSheet craMakeSheet) {

        return craftReceiveMapper.upadteCraSheetProNum(craMakeSheet);
    }

    /**
     * 修改工艺接收表
     *
     * @param carMakeResultModel
     */
    public void updateCraDetail(List<CarMakeResultModel> carMakeResultModel, Integer sheetId) {
        int n = 0;
        for (int i = 0; i < carMakeResultModel.size(); i++) {
            if (carMakeResultModel.get(i).getTechnologyChange() == 3) {
                //修改类型为新增
                List<CraMakeDetail> craMakeDetailList = craftReceiveMapper.selectMakeDetailByResultId(carMakeResultModel.get(i));
                if (craMakeDetailList.get(0).getMakeType().trim().equals("装配件")) {
                    craMakeDetailList.get(0).setIsNeedCard(0);
                } else {
                    craMakeDetailList.get(0).setIsNeedCard(1);
                }
                craMakeDetailList.get(0).setChangeType(2);
                craMakeDetailList.get(0).setLastTime(new Date());
                craMakeDetailList.get(0).setCreatName(ShiroUtils.getUser().getName());
                craftReceiveMapper.insertAddCraDetail(craMakeDetailList.get(0));
                n++;
            } else {
                //修改类型为修改或删除
                craftReceiveMapper.updateCraDetail(carMakeResultModel.get(i));
                ////修改删除isDel置位
                if (carMakeResultModel.get(i).getTechnologyChange() == 2){
                    craftReceiveMapper.updateCraDetailDel(carMakeResultModel.get(i));
                }
            }

        }
        //判断是否有增加的数据，如果有更新排序字段
        if (n > 0) {
            List<CraMakeDetail> craMakeDetailList = craftReceiveMapper.selectMakeDetailListByResultId(sheetId);
            if (craMakeDetailList.size() > 0) {

                //更新未拆分数据排序
                craftReceiveMapper.updateOrderNum(craMakeDetailList.get(0).getSheetId());
                //更新拆分数据排序
                craftReceiveMapper.updateOrderPid(craMakeDetailList.get(0).getSheetId());
            }
        }
    }

    /**
     * 查询接收的图纸变更接收明细
     *
     * @param serviceTaskModel
     * @return
     */
    public List<DrawChange> SelectDrawChangeBySheetId(ServiceTaskModel serviceTaskModel) {
        return craftReceiveMapper.SelectDrawChangeBySheetId(serviceTaskModel);
    }

    /**
     * 查询接收的原始单明细
     *
     * @param serviceTaskModel
     * @return
     */
    public List<MakePlan> SelectMakePlanBySheetId(ServiceTaskModel serviceTaskModel) {
        return craftReceiveMapper.SelectMakePlanBySheetId(serviceTaskModel);
    }

    /**
     * 查询接收的目录变更单明细
     *
     * @param serviceTaskModel
     * @return
     */
    public List<MakeChange> SelectMakeChangeBySheetId(ServiceTaskModel serviceTaskModel) {
        return craftReceiveMapper.SelectMakeChangeBySheetId(serviceTaskModel);
    }

    /**
     * 通过ID 查询makesheet表
     * @param id
     * @return
     */
    public List<MakeSheet> selectMakeSheetById(Integer id) {
        return craftReceiveMapper.selectMakeSheetById(id);
    }

    /**
     * 通过项目编号，部分，类型 查询result表
     * @param serviceTaskModel
     * @return
     */
    public List<MakeResult> selectMakeResultByAll(ServiceTaskModel serviceTaskModel) {

        return   craftReceiveMapper.selectMakeResultByAll(serviceTaskModel);
    }
}
